<template>
  <div style="width: 100%;">
    <el-row :gutter="10" class="mb8" v-if="!disabled">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete">删除</el-button>
      </el-col>
    </el-row>

    <el-table :data="joinFileList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="材料名称" align="center" prop="fileName" />
      <el-table-column label="附件地址" align="center" prop="filePath" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <JoinFileForm ref="fileFormRef" @submitForm="submitForm"/>
  </div>
</template>

<script>

  import { listMixin } from "@/utils/mixins/listMixin";
  import { listJoinFile, delJoinFile, exportJoinFile } from "@/api/waitress/joinFile";
  import JoinFileForm from "./joinFileForm.vue"

  export default {
    name: "JoinFile",
    components:{ JoinFileForm },
    mixins:[ listMixin ],
    data() {
      return {
        // 入伙材料表格数据
        joinFileList: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          joinRegisterId: null,
          fileName: null,
          filePath: null,
        },
        selection:[]
      };
    },
    watch:{
      'joinRegisterId'(v){
        this.getList();
      }
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询入伙材料列表 */
      getList() {
        this.queryParams.joinRegisterId =  this.joinRegisterId ||-1
        listJoinFile(this.queryParams).then(response => {
          this.joinFileList = response.rows;
          this.total = response.total;
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.single = selection.length!==1
        this.multiple = !selection.length
        this.selection = selection
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.$refs.fileFormRef.show({"type":"ADD"});
      },
      /** 删除按钮操作 */
      handleDelete() {
        let me = this;
        let fileName = this.selection.map(item => item.fileName)
        this.$confirm('是否确认删除入伙材料为"' + fileName + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return me.delJoinFiles();
        }).then(() => {
          this.msgSuccess("删除成功");
        }).catch(() => {});
      },
      delJoinFiles(){
        for(let i in this.selection){
          let item = this.selection[i]
          let index = this.joinFileList.findIndex(x=>x == item)
          if(index != -1){
            this.joinFileList.splice(index,1)
          }
        }
      },
      submitForm(row){
        this.joinFileList.push({
          fileId: row.fileId,
          joinRegisterId: this.joinRegisterId,
          fileName: row.fileName,
          filePath: row.filePath,
        })
      },
      getFileLsit(){
        return this.joinFileList
      }
    },
    props: {
      joinRegisterId: {
        type: String,
        default: ""
      },
      disabled:{
        type:Boolean,
        default:false
      }
    }
  };
</script>
